import './tab_content.css';

const tab_content = document.getElementById('tab-content');



export default (title) => {
    //先判断这个内容是否被载入过
    let key = false;
    [...tab_content.children].forEach(item => {
        if (item.dataset.title == title) {
            key = true;
            item.style.display = 'block';
        } else {
            item.style.display = 'none';
        }
    })

    if (!key && title != '') {
        //创建
        const div = document.createElement('div');
        div.dataset.title = title;

        import(`../${title}/${title}.tmp`).then(result => {
            div.innerHTML = result.default;
        })
        div.style.display = 'block';
        import(`../${title}/${title}.js`).then(result => {
            // console.log(result.default)
            result.default();

        });
        import(`../${title}/${title}.js`);
        import(`../${title}/${title}.css`);

        tab_content.appendChild(div);
        if (module.hot) {
            module.hot.accept(`../${title}/${title}.js`);
            module.hot.accept(`../${title}/${title}.tmp`);
            module.hot.accept(`../${title}/${title}.css`);
        }

    }

}


export function removeContent(title) {
    const elem = tab_content.querySelector(`[data-title="${title}"]`);
    if (elem) {
        elem.remove()
    }
}